import 'package:flutter/material.dart';
import 'package:flutter_enjoy_plus/api/home.dart';
import 'package:flutter_html/flutter_html.dart';

class NoticeDetail extends StatefulWidget {
  const NoticeDetail({super.key});

  @override
  State<NoticeDetail> createState() => _NoticeDetailState();
}

class _NoticeDetailState extends State<NoticeDetail> {
  Map notifyDetail = {
    "id": "",
    "content": "",
    "title": "",
    "createdAt": "",
    "creatorName": ""
  };

  // @override
  // void initState() {
  //   // TODO: implement initState
  //   super.initState();
  // }

//? 比如想获取上一个页面的路由传参 -didChangeDependencies
//? 如果依赖的数据发生了变化，比如某些主题或者是路由传参，统一在didChangeDependencies中获取
  @override
  void didChangeDependencies() {
    // TODO: implement didChangeDependencies
    super.didChangeDependencies();
    //? 获取公告id
    // final data = ModalRoute.of(context)?.settings.arguments as Map;
    // getAnnouceDetail(data['id']);
    final data = ModalRoute.of(context)?.settings.arguments;
    if (data != null) {
      getAnnouceDetail((data as Map<String, dynamic>)['id']);
    }
  }

  //? 发送请求
  getAnnouceDetail(String id) async {
    final res = await getAnnounceDetailAPI(id);
    setState(() {
      notifyDetail = res;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: const Text('公告详情'),
        ),
        body: ListView(children: [
          Container(
              padding: const EdgeInsets.all(10),
              child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    // 标题
                    Text(notifyDetail['title'] ?? '',
                        style: const TextStyle(
                          fontSize: 18,
                          fontWeight: FontWeight.bold,
                        )),
                    const SizedBox(height: 10),
                    Row(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        children: [
                          Text(notifyDetail['creatorName'] ?? '',
                              style: const TextStyle(color: Colors.grey)),
                          Text(notifyDetail['createdAt'] ?? '',
                              style: const TextStyle(color: Colors.grey))
                        ]),
                    const SizedBox(height: 10),
                    // 内容
                    Html(
                      data: notifyDetail['content'] ?? "",
                    )
                  ]))
        ]));
  }
}
